<template>
	<div class="Essay-components">
		<div v-if="data.questionAnswerContent && explain">
			<p class="ques-title">{{ data.questionAnswerContent.question }}</p>
			<div class="input-item">
				<van-field
					disabled
					v-model="answer"
					rows="2"
					autosize
					type="textarea"
					maxlength="1000"
					show-word-limit
				/>
			</div>
			<p
				class="ques-title"
				style="font-weight: bold"
				v-show="!data.questionAnswerContent.score"
			>
				正确答案：
				<span style="color: #56baca">{{ data.questionAnswerContent.answer }}</span>
			</p>
		</div>
		<div v-if="data.content && !explain">
			<p class="ques-title">{{ data.content.question }}</p>
			<div class="input-item">
				<van-field
					v-model="answer"
					rows="2"
					autosize
					type="textarea"
					maxlength="1000"
					show-word-limit
				/>
			</div>
			<slot></slot>
		</div>
	</div>
</template>
<script>
export default {
	name: "Work",
	props: {
		data: {
			type: Object,
			default: {},
		},
		explain: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			answer: "",
			type: {
				TypeWork: "应用题",
				TypeSubjective: "主观题",
				TypeExplanation: "名词解释题",
			},
		};
	},
	watch: {
		data: {
			handler(newV, oldV) {
				this.initState();
			},
			deep: true,
		},
	},
	mounted() {
		this.initState();
	},

	methods: {
		initState() {
			if (this.explain) {
				if (this.data.questionAnswerContent) {
					this.answer = this.data.questionAnswerContent.studentAnswer;
				} else {
					this.filterRight();
					this.answer = "";
				}
			} else {
				this.answer = this.data.answer;
			}
		},
		filterRight() {
			this.data.questionAnswerContent = JSON.parse(JSON.stringify(this.data.questionContent));
			this.data.questionAnswerContent.studentAnswer = "";
		},
		computeScore() {
			var content = JSON.parse(JSON.stringify(this.data.content));
			content.studentAnswer = this.answer;
			content.finish = this.$isFull(this.answer);
			return { content };
		},
		changeValue(key) {
			var { content } = this.computeScore();
			this.$emit("changeValue", this.answer, content, this.score);
		},
	},
};
</script>

<style lang="scss">
.Essay-components {
}
</style>
